package com.fy.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.jwt.JWT;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fy.pojo.User;
import com.fy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }
    // 密钥
    byte[] key = "1234567890".getBytes();
    @RequestMapping("login")
    public String login(String username, String password, Model model, HttpServletResponse response){
        if(StringUtils.isEmpty(username)){
            model.addAttribute("error","用户名不能为空");
            return "login";
        }
        if(StringUtils.isEmpty(password)){
            model.addAttribute("error","密码不能为空");
            return "login";
        }
        User user = userService.getUser(username);
        if(user==null){
            model.addAttribute("error","用户名不存在");
            return "login";
        }
        if(!password.equals(user.getPassword())){
            model.addAttribute("error","密码不正确");
            return "login";
        }

        String token = JWT.create()
                .setPayload("username", username)
                .setPayload("id", user.getId())
                .setKey(key)
                .sign();
        Cookie cookie=new Cookie("login_token",token);
        cookie.setPath("/");
        cookie.setMaxAge(60*30);
        response.addCookie(cookie);

        return "redirect:/main/list";
    }
    @RequestMapping("auth")
    public String auth(String code,HttpServletResponse response){
        String url="https://gitee.com/oauth/token";
        //POST请求
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("grant_type", "authorization_code");
        paramMap.put("code", code);
        paramMap.put("client_id", "6008f3187a7a81a37b80f70570dd1d285b06bbaf706797f45d86e185b23bc8bc");
        paramMap.put("redirect_uri", "http://localhost:8080/user/auth");
        paramMap.put("client_secret", "2899fbc952aa7d6d673b57d42f9245bceffe64a4064702994201b03af2961075");
        String access_token = HttpUtil.post(url, paramMap);
        JSONObject jsonObject = JSON.parseObject(access_token);
        Object token = jsonObject.get("access_token");

        String url2="https://gitee.com/api/v5/user?access_token="+token;
        String s = HttpUtil.get(url2);
        JSONObject jsonObject1 = JSON.parseObject(s);
        Object username = jsonObject1.get("name");
        User user = userService.getUser(username.toString());
        if(user==null){
            user=new User();
            user.setUsername(username.toString());
            userService.add(user);
        }

        String token2 = JWT.create()
                .setPayload("username", username)
                .setPayload("id", user.getId())
                .setKey(key)
                .sign();
        Cookie cookie=new Cookie("login_token",token2);
        cookie.setPath("/");
        cookie.setMaxAge(60*30);
        response.addCookie(cookie);

        return "redirect:/main/list";
    }
}
